package com.study.sxc.controller;

import com.study.sxc.Entity.Lab;
import com.study.sxc.enums.ResponseCode;
import com.study.sxc.response.Response;
import com.study.sxc.service.labService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api/lab")
public class LabController {

    @Resource
    private labService labService;

    @GetMapping("/get")
    public Response<List<Lab>> getLabs() {
        // 查询所有机房
        List<Lab> labs = labService.getAllLabs();
        if (labs == null || labs.isEmpty()) {
            return Response.<List<Lab>>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(labs)
                    .build();
        }
        return Response.<List<Lab>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(labs)
                .build();
    }

    @PostMapping("/add")
    public Response<Void> addLab(@RequestBody Lab lab) {
        // 打印接收到的参数，便于排查
        System.out.println("DEBUG: labName=" + lab.getLabName() + ", location=" + lab.getLocation() + ", capacity=" + lab.getCapacity());
        if (lab.getLabName() == null || lab.getLocation() == null || lab.getCapacity() == null) {
            System.out.println("ERROR: 参数有空值，labName=" + lab.getLabName() + ", location=" + lab.getLocation() + ", capacity=" + lab.getCapacity());
            return Response.<Void>builder()
                    .code("9999")
                    .info("参数不能为空")
                    .build();
        }
        try {
            int result = labService.addLab(lab);
            if (result > 0) {
                return Response.<Void>builder()
                        .code(ResponseCode.SUCCESS.getCode())
                        .info(ResponseCode.SUCCESS.getInfo())
                        .build();
            } else {
                System.out.println("ERROR: 插入数据库失败，labName=" + lab.getLabName() + ", location=" + lab.getLocation() + ", capacity=" + lab.getCapacity());
                return Response.<Void>builder()
                        .code(ResponseCode.UN_ERROR.getCode())
                        .info(ResponseCode.UN_ERROR.getInfo())
                        .build();
            }
        } catch (Exception ex) {
            System.out.println("EXCEPTION: " + ex.getMessage());
            ex.printStackTrace();
            return Response.<Void>builder()
                    .code("9998")
                    .info("后端异常: " + ex.getMessage())
                    .build();
        }
    }

    @PutMapping("/update")
    public Response<Void> updateLab(@RequestBody Lab lab) {
        int result = labService.updateLab(lab);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @DeleteMapping("/delete")
    public Response<Void> deleteLab(@RequestBody Lab lab) {
        Integer id = lab.getLabId();
        // 校验id
        if (id == null || id <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("ID不能为空且必须为正整数")
                    .build();
        }
        int result = labService.deleteLab(id);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @GetMapping("/get/{id}")
    public Response<Lab> getLabById(@PathVariable Integer id) {
        // 根据id获取机房信息
        Lab lab = labService.getLabById(id);
        if (lab == null) {
            return Response.<Lab>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(null)
                    .build();
        }
        return Response.<Lab>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(lab)
                .build();
    }
}
